package com.api.weavernorth;

import com.weavernorth.sysap.util.SyncUtil;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.Util;
import javax.servlet.ServletException;

import java.io.IOException;


public class EditHrmMessageController  {

    private static Log newlog = LogFactory.getLog(EditHrmMessageController.class);
    //更新信息
    public JSONObject editmess(String userMess)
            throws ServletException, IOException {
        //返回信息
        JSONObject jsonObject = new JSONObject();
        SyncUtil syncUtil=new SyncUtil();

        //传入的信息
        if (!"".equals(userMess)) {
            StringBuffer upSql = new StringBuffer();

            JSONObject json = JSONObject.fromObject(userMess);
            newlog.info("传入的更新的数据:" + json.toString());
            //用户编码
            String strWorkcode = Util.null2String(json.get("workcode"));
            if ("".equals(strWorkcode)) {
                jsonObject.put("status", "1");
                jsonObject.put("message", "人员更新失败,编码不允许为空");
                return jsonObject;
            }
            upSql.append("update hrmresource set  workcode='" + strWorkcode + "'");
            //手机号
            String strMobile = Util.null2String(json.get("mobile"));
            if (!"".equals(strMobile)) {
                upSql.append(" ,mobile='" + strMobile + "'");
            }

            //邮箱
            String strEmail = Util.null2String(json.get("email"));
            if (!"".equals(strEmail)) {
                upSql.append(" ,email='" + strEmail + "'");
            }
            //登录账号
            String strLoginid=Util.null2String(json.get("loginid"));
            if (!"".equals(strLoginid)) {
                upSql.append(" ,loginid='" + strLoginid + "'");
            }
            upSql.append(" where workcode='" + strWorkcode + "' and (accounttype=0 or accounttype is null)");
            newlog.info("当前更新手机号邮箱信息sql:" + upSql.toString());
            //更新语句
            RecordSet rs = new RecordSet();
            boolean flag = rs.executeUpdate(upSql.toString());
            newlog.info("更新状态:" + flag);
            //更新失败
            if (!flag) {
                jsonObject.put("status", "1");
                jsonObject.put("message", "更新手机号邮箱信息失败");

            } else {
                //更新完成后清除缓存
                syncUtil.clearCache(3);
                jsonObject.put("status", "0");
                jsonObject.put("message", "更新手机号邮箱信息成功");
            }


        }
        return jsonObject;

    }


}
